<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>Binance 交易图表</span>
        <div style="display: flex; align-items: center; gap: 10px;">
          <!-- 实时价格显示 -->
          <div v-if="binanceCurrentPrice > 0" style="display: flex; align-items: center; gap: 8px;">
            <el-icon color="#1890ff" size="16"><Connection /></el-icon>
            <span style="font-size: 12px; color: #666;">实时价格:</span>
            <el-tag type="success" size="small" style="font-weight: bold;">
              ${{ binanceCurrentPrice.toLocaleString() }}
            </el-tag>
          </div>
          <el-select :value="binanceSymbol" placeholder="选择交易对" size="small" @change="updateBinanceChart">
            <el-option
              v-for="item in symbols"
              :key="item.value"
              :label="item.label"
              :value="item.value.replace('/', '')">
            </el-option>
          </el-select>
        </div>
      </div>
    </template>
    <div class="chart-container">
      <webview 
        ref="binanceWebview"
        :src="`https://www.binance.com/zh-CN/trade/${binanceSymbol}?theme=dark&type=spot`"
        class="binance-webview"
        allowpopups
        allow="autoplay; fullscreen"
        webpreferences="contextIsolation=no"
      ></webview>
    </div>
  </el-card>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue'
import { Connection } from '@element-plus/icons-vue'

export default {
  name: 'BinanceChart',
  components: {
    Connection
  },
  props: {
    binanceSymbol: {
      type: String,
      required: true
    },
    binanceCurrentPrice: {
      type: Number,
      required: true
    },
    symbols: {
      type: Array,
      required: true
    }
  },
  emits: ['update:binanceSymbol'],
  setup(props, { emit }) {
    const binanceWebview = ref(null)

    const updateBinanceChart = (newSymbol) => {
      emit('update:binanceSymbol', newSymbol)
    }

    return {
      binanceWebview,
      updateBinanceChart
    }
  }
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chart-container {
  width: 100%;
  height: 400px;
}

.binance-webview {
  width: 100%;
  height: 700px;
  border: none;
  display: inline-flex;
}
</style>